package com.bdqn.ordersystem.controller;

import com.bdqn.ordersystem.pojo.Order;
import com.bdqn.ordersystem.pojo.User;
import com.bdqn.ordersystem.service.AreaService;
import com.bdqn.ordersystem.service.CenterService;
import com.bdqn.ordersystem.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;
    @Resource
    private AreaService areaService;
    @Resource
    private CenterService centerService;



    @RequestMapping("/list")
    public String list(Model model){
//        model.addAttribute("list",orderService.listAll());
        return "/handle/hello";
    }

    @RequestMapping("/listByCondition")
    public String listBy(Model model,HttpSession session,String flowId, @PathVariable("areaId")Integer areadId, @PathVariable("centerId")Integer centerId,
                     @PathVariable("address")String address,@PathVariable("receiveBy")Integer receiveBy,@PathVariable("status")Integer status   ){
        Order order=new Order();
        order.setFlowId(flowId);
        order.setAreaId(areadId);
        order.setCenterId(centerId);
        order.setAddress(address);
        if(((User)session.getAttribute("logoUser")).getId()==0){
            order.setReceiveBy(receiveBy);
            order.setStatus(status);
        }

        model.addAttribute("list",orderService.listByCondition(order));
        return "";
    }

    @GetMapping("/add")
    public String add(Model model){
        model.addAttribute("areaList",areaService.listAll());
        return "handle/add";
    }
    @PostMapping("/add")
    public String add(Model model,HttpSession session, Order order){
        order.setCreatedBy(((User)session.getAttribute("loginUser")).getId());
        order.setCreationDate(new Date());
        if(orderService.add(order)) {
            return "redirect/plans";
        }else{
            model.addAttribute("order",order);
            return "pages/all-admin-404";
        }
    }
    @GetMapping("/modify")
    public String modifyOrder(Model model,Integer id){
        model.addAttribute("centerList",centerService.listAll());
        model.addAttribute("areaList",areaService.listAll());
        model.addAttribute("order",orderService.get(1));

        return "handle/modify";
    }

    @RequestMapping("/modifyorderRecevie")
    public Boolean modifyOrderStatus(Integer id,String recevie){
        orderService.modifyRecevie(id,recevie);
        return null;
    }

    @RequestMapping("/orderEnd")
    public Boolean modifyOrderEnd(Integer id,String resume,String logPath){
        orderService.modifyEnd(id,resume,logPath);
        return null;
    }

    @RequestMapping("/status")
    public Boolean modifyStatus(Integer id,Integer status){
        orderService.modifyStatus(id,status);
        return null;
    }

    @RequestMapping("/del")
    public Boolean del(@PathVariable("id")Integer id){
        return orderService.remove(id);
    }



}